package com.xbongbong.parent.parse.data;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.AnalysisDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CalendarUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.BirthdayTypeEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.help.FormDataAnalysisHelp;
import com.xbongbong.saas.help.ShowFormatHelp;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 解析所有的字段，用于除详情、编辑、列表、关联查询的回显(新格式带颜色)
 * @author: wufeng
 * @date: 2018/12/11 15:17
 * @desrcption:
 */
@Component
public class FormDataDetailAnalysisDataHelp extends FormDataGetAnalysisDataHelp {

    @Resource
    private FormDataAnalysisHelp formDataAnalysisHelp;
    @Resource
    private ShowFormatHelp showFormatHelp;

    /**
     * 之所以不使用FormDataGetAnalysisDataHelp是因为，在我新建的这个类时间为止，其方法以下复用的太多，因此，不再修改类FormDataGetAnalysisDataHelp
     * @date 2019/7/4 15:23
     *
     */
    @Override
    public void analysisData(AnalysisDataDTO analysisDataDTO, JSONObject resultJsonObject, Map<String, Object> linkData
                    ,Set<Object> linkDataSet) throws XbbException {
        // TODO 关于成员和部门回显需要去掉离职人员，现在直接不限，具体如何显示，未解决
        FieldAttrEntity fieldAttrEntity = analysisDataDTO.getFieldAttrEntity();
        JSONObject objectJsonObject = analysisDataDTO.getDataJsonObject();

        if (FieldTypeEnum.RADIO_GROUP.getType().equals(fieldAttrEntity.getFieldType()) || FieldTypeEnum.COMBO.getType().equals(fieldAttrEntity.getFieldType())) {
            if (Objects.equals(fieldAttrEntity.getSaasAttr(), SelectProductEnum.UNIT.getSaasAttr()) || Objects.equals(fieldAttrEntity.getSaasAttr(), ProductEnum.UNIT.getSaasAttr())) {
                // 产品单位特殊处理下
                formDataAnalysisHelp.analysisCombo(objectJsonObject, resultJsonObject,fieldAttrEntity, analysisDataDTO.getCorpid());
            } else {
                //单选、下拉单选返回带颜色的ItemPoJO
                formDataAnalysisHelp.analysisComboWithColor(objectJsonObject, resultJsonObject,fieldAttrEntity, analysisDataDTO.getCorpid());
            }
        } else if (Objects.equals(FieldTypeEnum.STAGE_THRUSTER.getType(), fieldAttrEntity.getFieldType())) {
            formDataAnalysisHelp.analysisListStageJsonObjectWithColor(resultJsonObject, analysisDataDTO);
        } else if (FieldTypeEnum.CHECKBOX_GROUP.getType().equals(fieldAttrEntity.getFieldType())
                || FieldTypeEnum.COMBO_CHECK.getType().equals(fieldAttrEntity.getFieldType())) {
            // 多选转化成逗号隔开的字符串
            formDataAnalysisHelp.analysisComboReturnStrWithColor(objectJsonObject, resultJsonObject, fieldAttrEntity);
        } else if (Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.LINK_BUSINESS_MULTI.getType()) || Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.LINK_BUSINESS_SINGLE.getType())) {
            // saas关联业务的特殊逻辑
            showFormatHelp.formatListBusiness4ShowBase(resultJsonObject, fieldAttrEntity.getAttr(), fieldAttrEntity.getLinkedText(), fieldAttrEntity.getFieldType(), fieldAttrEntity.getLinkedType(), analysisDataDTO.getBusinessType(), analysisDataDTO.getCorpid());
        } else if (FieldTypeEnum.BIRTHDAY.getType().equals(fieldAttrEntity.getFieldType())) {
            // 获取生日是公历还是阴历

            Integer flag = objectJsonObject.getInteger(fieldAttrEntity.getLinkedText());
            String birthday = objectJsonObject.getString(fieldAttrEntity.getAttr());
            if (!DateTimeUtil.isSDFDate(birthday)) {
                return;
            }
            JSONObject jsonObject = new JSONObject();
            if (Objects.equals(flag, BirthdayTypeEnum.LUNAR_BIRTHDAY.getValue())) {
                // 阴历生日
                jsonObject.put(StringConstant.BIRTHDAY_FLAG, flag);
                jsonObject.put(StringConstant.BIRTHDAY, birthday);
                jsonObject.put(StringConstant.VALUE, CalendarUtil.lunar2Str(birthday));
            } else if (Objects.equals(flag, BirthdayTypeEnum.BIRTHDAY.getValue())) {
                // 公历生日
                jsonObject.put(StringConstant.BIRTHDAY_FLAG, flag);
                jsonObject.put(StringConstant.BIRTHDAY, birthday);
                jsonObject.put(StringConstant.VALUE, birthday);
            }
            resultJsonObject.put(fieldAttrEntity.getAttr(), jsonObject);
        }else if (FieldTypeEnum.STAR.getType().equals(fieldAttrEntity.getFieldType())) {
            resultJsonObject.put(fieldAttrEntity.getAttr(), objectJsonObject.getInteger(fieldAttrEntity.getAttr()));
        } else {
            super.analysisData(analysisDataDTO, resultJsonObject, linkData, linkDataSet);
        }

    }
}
